package com.tuyue.police.biz.impl;

import com.tuyue.police.bean.DeviceBean;
import com.tuyue.police.biz.IDeviceBiz;
import com.tuyue.police.biz.ISectorBiz;
import com.tuyue.police.dao.*;
import com.tuyue.police.enums.ResultEnum;
import com.tuyue.police.exception.MyException;
import com.tuyue.police.pojo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 王金海
 * @Description:
 * @Date: Created by Administrator on 2018/3/14.
 * @Modified By:
 */
@Service
public class DeviceBizImpl implements IDeviceBiz {

    @Autowired
    private IUserDao userDao;
    @Autowired
    private IDeviceDao deviceDao;
    @Autowired
    private ISectorDao sectorDao;
    @Autowired
    private IEnterpriseDao enterpriseDao;
    @Autowired
    private IDeviceTypeDao deviceTypeDao;

    @Autowired
    private ISectorBiz sectorBiz;
    @Transactional
    @Override
    public DeviceBean add(DeviceBean deviceBean) {
        List<Long> sIds = deviceBean.getSectorIds();
        if (!ObjectUtils.isEmpty(sIds)) {
            sectorBiz.delSector(sIds);
        }
        Enterprise enterprise = enterpriseDao.findOne(deviceBean.getDeviceEnterpriseId());
        if (ObjectUtils.isEmpty(enterprise)) {
            throw new MyException(ResultEnum.ENTERPRISE_ERROR);
        }
        Device device = new Device();
        BeanUtils.copyProperties(deviceBean, device);
        Device save = deviceDao.save(device);
        List<Sector> sectors = deviceBean.getSectors();
        List<Sector> sectorList = new ArrayList<>();
        if (sectors != null) {
            ArrayList<Sector> arrayList = new ArrayList<>();
            sectors.forEach(sector -> {
                sector.setSectorDeviceId(save.getDeviceId());
                sector.setSectorFlag(0);
                arrayList.add(sector);
            });
            sectorList = sectorDao.save(arrayList);
        }
        DeviceBean bean = new DeviceBean();
        BeanUtils.copyProperties(save, bean);
        bean.setSectors(sectorList);

        return bean;
    }

    @Override
    public Page<Device> findByUserId(Long userId, Pageable pageable) {
        User one = userDao.findOne(userId);
        if (ObjectUtils.isEmpty(one)) {
            throw new MyException(ResultEnum.USER_ERROR);
        }

        List<Long> ids = enterpriseDao.findByUserId(userId);


        Page<Device> deviceList = deviceDao.findAll(new Specification<Device>() {
            @Override
            public Predicate toPredicate(Root<Device> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<>();
                if (ids != null && ids.size() > 0) {
                    CriteriaBuilder.In<Object> in = cb.in(root.get("deviceEnterpriseId"));
                    for (Long id : ids) {
                        in.value(id);
                    }
                    list.add(in);
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        }, pageable);
        List<Device> devices = new ArrayList<>();
        for (Device device : deviceList.getContent()) {
            device.setDeviceSectorSize(sectorDao.findAllBySectorDeviceId(device.getDeviceId()).size());
            if (device.getDeviceEnterpriseId() != 0) {
                Enterprise enterprise = enterpriseDao.getOne(device.getDeviceEnterpriseId());
                if (enterprise != null) {
                    device.setEname(enterprise.geteName());
                }
            }

            if (device.getDeviceTypeId() != 0) {
                DeviceType deviceType = deviceTypeDao.getOne(device.getDeviceTypeId());
                if (deviceType != null) {
                    device.setDeviceTypeName(deviceType.getDeviceTypeName());
                }
            }

            devices.add(device);
        }
        return new PageImpl<Device>(devices, pageable, deviceList.getTotalElements());
    }

    @Override
    public DeviceBean findById(Long deviceId) {
        Device device = deviceDao.findOne(deviceId);
        if (ObjectUtils.isEmpty(device)) {
            throw new MyException(ResultEnum.FACILITY_ISNOTEXIST);
        }
        DeviceBean deviceBean = new DeviceBean();
        BeanUtils.copyProperties(device, deviceBean);
        List<Sector> sectorList = sectorDao.findAllBySectorDeviceId(device.getDeviceId());
        deviceBean.setSectors(sectorList);
        return deviceBean;
    }


}
